home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / mail / pine3.96.tar.gz / pine3.96.tar / pine3.96 / pine / osdep / tempnam.dos < prev    next >
Text File  |  1996-05-22  |  4KB  |  148 lines

  1. #line 2 "osdep/tempnam.dos"
  2. /*
  3.  * This routine is derived from BSD4.3 code,
  4.  * Copyright (c) 1988 Regents of the University of California.
  5.  * All rights reserved.
  6.  */
  7. #if defined(LIBC_SCCS) && !defined(lint)
  8. static char sccsid[] = "@(#)tmpnam.c    4.5 (Berkeley) 6/27/88";
  9. #endif /* LIBC_SCCS and not lint */
  10.  
  11.  
  12.  
  13. /*
  14.  * Module global.
  15.  */
  16. static char *NumTemplate = "XXXXXX";
  17.  
  18.  
  19. /*----------------------------------------------------------------------
  20.       Return a unique file name in a given directory.  This is not quite
  21.       the same as the usual tempnam() function, though it is very similar.
  22.       We want it to use the TMP environment variable only if dir is NULL,
  23.       instead of using TMP regardless if it is set.
  24.  
  25.   Args: dir      -- The directory to create the name in
  26.         prefix   -- Prefix of the name
  27.  
  28.  Result: Malloc'd string equal to new name is returned.  It must be free'd
  29.      by the caller.  Returns the string on success and NULL on failure.
  30.   ----*/
  31. static char *
  32. temp_mkbase (dir, prefix)
  33.     char *dir, *prefix;
  34. {
  35.     struct stat buf;
  36.     char *f, *name;
  37.     char *mktemp();
  38.  
  39.     if (!(name = malloc((unsigned int)MAXPATH)))
  40.         return(NULL);
  41.  
  42.     if (!dir && ((f = getenv("TMP")) || (f = getenv("TEMP"))) &&
  43.              !stat(f, &buf) && (buf.st_mode&S_IFMT) == S_IFDIR &&
  44.              !can_access(f, WRITE_ACCESS)) {
  45.         (void)strcpy(name, f);
  46.         goto done;
  47.     }
  48.  
  49.     if (dir) {
  50.     strcpy(name, dir);
  51.     if(!*dir || (isalpha(*dir) && *(dir+1) == ':' && !*(dir+2)))
  52.       strcat(name, "\\");
  53.  
  54.     if((!stat(name, &buf) && (buf.st_mode&S_IFMT) == S_IFDIR)
  55.             && !can_access(name, WRITE_ACCESS)) {
  56.             (void)strcpy(name, dir);
  57.             goto done;
  58.     }
  59.     }
  60.  
  61. #ifndef P_tmpdir
  62. #define    P_tmpdir    "\\tmp"
  63. #endif
  64.     if (!stat(P_tmpdir, &buf) &&
  65.                          (buf.st_mode&S_IFMT) == S_IFDIR &&
  66.              !can_access(P_tmpdir, WRITE_ACCESS)) {
  67.         (void)strcpy(name, P_tmpdir);
  68.         goto done;
  69.     }
  70.  
  71.     free(name);
  72.     return(NULL);
  73.  
  74. done:
  75.     if(name[0] && name[strlen(name)-1] != '\\')
  76.       (void)strcat(name, "\\");
  77.  
  78.     if (prefix)
  79.         (void)strcat(name, prefix);
  80.     (void)strcat(name, NumTemplate);
  81.     return(name);
  82. }
  83.  
  84.  
  85.  
  86. char *
  87. temp_nam (dir, prefix)
  88.     char *dir, *prefix;
  89. {
  90.     char *name;
  91.  
  92.     if ((name = temp_mkbase (dir, prefix)))
  93.     return (mktemp (name));
  94.     return (NULL);
  95. }
  96.     
  97.  
  98. /*----------------------------------------------------------------------
  99.  
  100.       Like temp_nam but create a unique name _with_ an extension.
  101.  
  102.   Args: dir      -- The directory to create the name in
  103.         prefix   -- Prefix of the name
  104.     ext     -- Extension to append.
  105.  
  106.  Result: Malloc'd string equal to new name is returned.  It must be free'd
  107.      by the caller.  Returns the string on success and NULL on failure.
  108.   ----*/
  109. char *
  110. temp_nam_ext (dir, prefix, ext)
  111.     char *dir, *prefix, *ext;
  112. {
  113.     char *name;
  114.     char *numberpos;
  115.     int     tries;
  116.     int     pid;
  117.  
  118.     name = temp_mkbase (dir, prefix);
  119.  
  120.     if (name == NULL)                /* Failed to make base name? */
  121.     return (NULL);
  122.  
  123.     if (ext == NULL || *ext == '\0')        /* No extension required? */
  124.     return (mktemp(name));
  125.  
  126.     if ((numberpos = strstr (name, NumTemplate)) == NULL) {
  127.     free (name);
  128.     return (NULL);
  129.     }
  130.  
  131.     /*
  132.      * Start with the PID.  Build file names, incrementing the pid
  133.      * until we encounter a unique file name.  Don't keep trying for ever.
  134.      */
  135.     pid = getpid ();
  136.     tries = 1000;
  137.     do {
  138.     sprintf (numberpos, "%06d.%s", pid++, ext);
  139.     if (--tries == 0) {
  140.         free (name);
  141.         return (NULL);
  142.     }
  143.     } while (can_access (name, ACCESS_EXISTS) == 0);
  144.     return (name);
  145. }
  146.  
  147.  
  148.